#!/usr/bin/perl
# Version 0.2.1

$initial = 1;

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 1;

$filename = "report.log";
$testsuite = "Testsuite";

if ($#ARGV < 2) {
        print("Usage: verdict.pl test_suite_name report_filename dir1 ... dirn");
        exit 1;
}

foreach $dir (@ARGV) {
        if($initial eq 1) {
                $initial = 2;
                $testsuite = $dir;      
        } elsif ($initial eq 2) {
                $initial = 3;
                $filename = $dir;
        } else {
                $case_verdict = 3;
                
                $rep_file = "$dir/$filename";
                if(-f $rep_file) {
                        $success = 1;
                        open(ACT_FILE,$rep_file) or $success = 0;
                        
                        if($success eq 1) {
                                while(<ACT_FILE>) {
                                        if(/### VERDICT for (.*): (PASS|FAIL|INCONCLUSIVE) ###/) {
                                                print("$1: Result $2\n");
                                                if($2 eq "PASS") {
                                                        $case_verdict = 1;
                                                } elsif ($2 eq "FAIL") {
                                                        $case_verdict = 2;
                                                }                                                                                 
                                        }                                        
                                }
                 
                        }
                        
                        close(ACT_FILE);
                } else {
                        print("No report for test case $dir. Result INCONCLUSIVE.\n");
                }
                
                if($case_verdict eq 2) {
                        $verdict = 2;
                } elsif ($case_verdict eq 3) {
                        if($verdict eq 1) {
                                $verdict = 3;
                        }
                }
        }
}

print("### VERDICT for $testsuite: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");